package com.example.imserver.repository;

import com.example.imserver.entity.Contact;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;

@Repository
public interface ContactRepository extends JpaRepository<Contact, Long> {
    // 查找用户的所有联系人
    @Query("SELECT c FROM Contact c WHERE c.userId = :userId AND c.status = 'ACCEPTED' ORDER BY c.createdAt DESC")
    List<Contact> findByUserId(@Param("userId") Long userId);
    
    // 查找两个用户之间的联系人关系
    @Query("SELECT c FROM Contact c WHERE " +
           "(c.userId = :userId1 AND c.contactId = :userId2) OR " +
           "(c.userId = :userId2 AND c.contactId = :userId1)")
    Optional<Contact> findContactRelation(@Param("userId1") Long userId1, @Param("userId2") Long userId2);
    
    // 查找待处理的联系人请求
    @Query("SELECT c FROM Contact c WHERE c.contactId = :userId AND c.status = 'PENDING' ORDER BY c.createdAt DESC")
    List<Contact> findPendingRequests(@Param("userId") Long userId);
}

